<?php error_reporting(E_ALL^E_NOTICE^E_WARNING); ?>
<?php
	global $title;
?>
<?php $title = "小z短网址"; ?>

<html>
<head>
<meta charset = "UTF-8" />
<link rel="shortcut icon" href="http://pic.bsdev.cn/wp-content/themes/TangStyle_V1.0.8/images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="./css/style.css?abd" type="text/css" />
<title><?php echo $title ?></title>
</head>
<body>
<div id = "xieyi">
	<center><h3>使用协议</h3></center>
	<ol>
		<li>小z短网址请勿用于缩短非法网站，一经发现立即删除</li>
		<li>小z短网址仅用于学习与交流使用，可自由传播，但请勿用于其它非法用途，否则后果自负</li>
		<li>如果您正在使用，代表您遵守以上协议</li>
	</ol>
</div>
<div id = "show">
<?php
	include_once("config.php");
	
	$con = $db->connect();
	$db->select("dwz",$con);
	
	
	$url = $_POST['d_url'];
	$id = $_GET['id'];
	$sql = "INSERT INTO `d_url`(`url`) VALUES ('$url')";//写入数据库
	$se = "SELECT `id`, `url` FROM `d_url` WHERE `id` = $id";//查询ID和连接
	$seurl = "SELECT * FROM `d_url` WHERE `url`= '$url'";//查询是否存在此URL
	
	
	
	if(isset($_POST['sub'])) {
		$re = $db->query($seurl,$con);
		$num = $db->num($re);
		//如果数据库中已经存在同样的URL则直接输出
		if($num >= 1) {
			//获取域名或主机地址 
			$host = $_SERVER['HTTP_HOST']; 
			//获取目录
			$path = $_SERVER['PHP_SELF'];
			$path = str_replace("index.php","",$path);
			$tiao = $db->fetch_arr($re);
			$have = $host.$path."?id=".$tiao[id];
			echo "短网址为：".$have;
			echo "&nbsp;&nbsp;<a href = \"$hvae?id=$tiao[id]\" target = '_blank' rel = 'nofollow' />点击打开</a>";
		}
		//不存在则写入数据库
		else {
			$db->query($sql,$con);
			include_once("show.php");
		}
	}
	
	//当用户直接访问短网址时，进行跳转
	if(isset($_GET['id'])) {
		$title = "正在打开，请稍等...";
		$re = $db->query($se,$con);
		$tiao = $db->fetch_arr($re);
		echo "<script>document.location= '$tiao[url]'</script>";
	}
?>
<form action = "" method = "post" name = "myform" onsubmit = "return check();">
	<span style = "padding-bottom:20px;">请输入网址：</span><br />
	<input type = "url" name = "d_url" placeholder = "如：http://www.xiaoz.me" value = "<?php echo $url; ?>" />
	<input type = "submit" name = "sub" value = "缩 短" class = "btn" />
</form>
</div>
<div id = "banquan">&copy;2014 Powered by <a href = "http://www.xiaoz.me" title = "小z博客" target = "_blank">小z博客</a></div>
<script>
	function check() {
		var url = myform.d_url.value;
		if(url == "") {
			alert("URL不能为空");
			myform.d_url.focus();
			return false;
		}
	}
</script>
<!-- 百度分享 -->
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"2","bdPos":"right","bdTop":"100"},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到：","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
</body>
</html>